package cn.gcjxsc.service.merchant.recharge;

import cn.gcjxsc.common.data.query.merchant.RechargeRecordQuery;
import cn.gcjxsc.common.data.vo.RechargeRecordVo;
import cn.gcjxsc.entity.merchant.recharge.RechargeRecord;
import cn.gcjxsc.entity.merchant.recharge.RechargeRecord_;
import cn.gcjxsc.repository.merchant.recharge.RechargeRecordRepository;
import cn.gcjxsc.repository.specs.merchant.RechargeRecordSpec;
import cn.gcjxsc.service.CommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
@Service("merchantRechargeRecordService")
public class RechargeRecordService extends CommonService{
    @Autowired
    private RechargeRecordRepository recordRepository;

    public RechargeRecordVo converter(RechargeRecord record){
        RechargeRecordVo vo = new RechargeRecordVo();
        BeanUtils.copyProperties(record, vo);
        return vo;
    }

    @Transactional
    public List<RechargeRecord> saveOrUpdate(RechargeRecord... records) {
        List<RechargeRecord> rechargeRecords = Arrays.asList(records);
        return recordRepository.save(rechargeRecords);
    }

    public Page<RechargeRecord> findList(RechargeRecordQuery filter, int page, int pageSize) {
        PageRequest pageRequest = new PageRequest(page - 1, pageSize, new Sort(Sort.Direction.DESC, RechargeRecord_.created.toString()));
        return recordRepository.findAll(RechargeRecordSpec.buildSpec(filter), pageRequest);
    }
}
